<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图片路径" prop="chainImg">
        <el-input
          v-model="queryParams.chainImg"
          placeholder="请输入图片路径"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否删除(0-未删, 1-已删)" prop="isDeleted">
        <el-input
          v-model="queryParams.isDeleted"
          placeholder="请输入是否删除(0-未删, 1-已删)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['official:chain:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['official:chain:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['official:chain:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['official:chain:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="chainList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="排序" prop="orderNum"  width="60"></el-table-column>
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="描述内容" align="center" prop="content" />
      <el-table-column label="图片" >
        <template slot-scope="scope">
          <el-image style="height: 100px" :src="downloadApi+scope.row.chainImg" :preview-src-list="[downloadApi+scope.row.chainImg]" />
        </template>
      </el-table-column>
      <el-table-column label="图片路径" align="center" prop="chainImg" />
      <el-table-column label="备注" align="center" prop="remark" />
       <el-table-column prop="isDeleted" label="状态" width="80">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isDeleted"
            @change="(val) => updateStatus(scope.row, val)"
            :active-value="0"
            :inactive-value="1">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['official:chain:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['official:chain:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改官网友情链接对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="描述内容" prop="content">
          <el-input type="textarea" :rows="2" v-model="form.content" controls-position="right" />
        </el-form-item>
        <el-form-item label="显示排序" prop="orderNum">
          <el-input-number v-model="form.orderNum" controls-position="right" :min="1" />
        </el-form-item>
        <!-- <el-form-item label="图片路径" prop="chainImg">
          <el-input v-model="form.chainImg" placeholder="请输入图片路径" />
        </el-form-item> -->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="图片">
              <el-upload
                class="upload-demo"
                action="#"
                :limit="1"
                :show-file-list="true"
                ref="upload_projectImg"
                :auto-upload="false"
                :http-request="function(file){return submitProjectImg(file)}"
                list-type="picture">
                <el-button size="small" type="primary">点击上传</el-button>
              </el-upload>
            </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listChain, getChain, delChain, addChain, updateChain } from "@/api/official/chain";
import downloadApi, { uploadFuncAwait } from '@/utils/upload'

export default {
  name: "Chain",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 官网友情链接表格数据
      chainList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        content: null,
        orderNum:null,
        chainImg: null,
        isDeleted: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "描述内容不能为空", trigger: "blur" }
        ],
        orderNum: [
          { required: true, message: "显示顺序不能为空", trigger: "blur" }
        ]
      },
      projectImgFile:'',
      downloadApi: downloadApi
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /**
     * 获取上传路径
     * @param file
     */
    submitProjectImg(file) {
      this.projectImgFile = file.file;
    },
    /** 查询官网友情链接列表 */
    getList() {
      this.loading = true;
      listChain(this.queryParams).then(response => {
        this.chainList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        friendlyChainId: null,
        title: null,
        content: null,
        orderNum: null,
        chainImg: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        isDeleted:null
      };
      this.projectImgFile = ""
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.friendlyChainId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加友情链接";
      this.$refs.upload_projectImg.clearFiles()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const friendlyChainId = row.friendlyChainId || this.ids
      getChain(friendlyChainId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改友情链接";
        this.$refs.upload_projectImg.clearFiles()
      });
    },
     /** 变更状态操作 */
    updateStatus(row,val) {
      console.log(val)
        this.form = {...row};
        this.form.isDeleted = val
        updateChain(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.getList();
        });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(async valid => {
        if (valid) {
          let postUrlImg = '/upload/uploadImage'

          this.$refs.upload_projectImg.submit()
          if(this.projectImgFile)
          {
            let formData = new FormData()
            formData.append('multipartFile', this.projectImgFile)
            let ret = await uploadFuncAwait(postUrlImg, formData)
            this.$set(this.form, "chainImg", ret.data.msg)
          }
          
          if (this.form.friendlyChainId != null) {
            updateChain(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addChain(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const friendlyChainIds = row.friendlyChainId || this.ids;
      this.$modal.confirm('是否确认删除官网友情链接编号为"' + friendlyChainIds + '"的数据项？').then(function() {
        return delChain(friendlyChainIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('official/chain/export', {
        ...this.queryParams
      }, `chain_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
